home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 23 web forms and controls / databinding / pagingform.aspx.vb < prev    next >
Encoding:
Text File  |  2002-03-17  |  7.1 KB  |  185 lines

  1. Imports System.Data.OleDb
  2.  
  3. Public Class PagingForm
  4.     Inherits System.Web.UI.Page
  5.  
  6.     Protected WithEvents dgrTitles As System.Web.UI.WebControls.DataGrid
  7.  
  8. #Region " Web Form Designer Generated Code "
  9.  
  10.     'This call is required by the Web Form Designer.
  11.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  12.  
  13.     End Sub
  14.  
  15.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  16.         'CODEGEN: This method call is required by the Web Form Designer
  17.         'Do not modify it using the code editor.
  18.         InitializeComponent()
  19.     End Sub
  20.     Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
  21.     Protected WithEvents btnFirst As System.Web.UI.WebControls.Button
  22.     Protected WithEvents btnPrevious As System.Web.UI.WebControls.Button
  23.     Protected WithEvents btnNext As System.Web.UI.WebControls.Button
  24.     Protected WithEvents btnLast As System.Web.UI.WebControls.Button
  25.     Protected WithEvents txtPage As System.Web.UI.WebControls.TextBox
  26.     Protected WithEvents btnGo As System.Web.UI.WebControls.Button
  27.  
  28. #End Region
  29.  
  30.     ' Change this constant to enforce a different paging technique
  31.     '     0=default buttons, 1=custom buttons, 2=custom paging
  32. #Const PAGINGSTYLE = 2
  33.  
  34.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  35.         If Not Me.IsPostBack Then
  36.             ' Show the first page.
  37.             ShowPage(0)
  38.         End If
  39.     End Sub
  40.  
  41. #If PAGINGSTYLE = 0 Then
  42.     ' This event is used only when using default navigation controls.
  43.     Private Sub dgrTitles_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgrTitles.PageIndexChanged
  44.         dgrTitles.CurrentPageIndex = e.NewPageIndex
  45.         BindDataGrid()
  46.     End Sub
  47. #End If
  48.  
  49.     ' react to a click on a navigational button
  50.  
  51.     Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click, btnPrevious.Click, btnNext.Click, btnLast.Click, btnGo.Click
  52.         Dim pageNum As Integer = dgrTitles.CurrentPageIndex
  53.  
  54.         Select Case DirectCast(sender, Button).CommandName.ToLower
  55.             Case "first"
  56.                 pageNum = 0
  57.             Case "prev"
  58.                 pageNum -= 1
  59.             Case "next"
  60.                 pageNum += 1
  61.             Case "last"
  62.                 pageNum = dgrTitles.PageCount - 1
  63.             Case "go"
  64.                 Try
  65.                     ' Visible page number is 1-based.
  66.                     pageNum = CInt(txtPage.Text) - 1
  67.                 Catch
  68.                     Exit Sub
  69.                 End Try
  70.         End Select
  71.  
  72.         ' Show this page.
  73.         ShowPage(pageNum)
  74.     End Sub
  75.  
  76.     ' Display a page.
  77.     Sub ShowPage(ByVal pageNum As Integer)
  78.         ' Keep the page number in valid range.
  79.         pageNum = Math.Max(0, Math.Min(pageNum, dgrTitles.PageCount - 1))
  80.         ' Enforce the new page number .
  81.         dgrTitles.CurrentPageIndex = pageNum
  82.  
  83.         ' Bind the control.
  84.         BindDataGrid()
  85.  
  86.         ' Update button state.
  87.         btnFirst.Enabled = (pageNum > 0)
  88.         btnPrevious.Enabled = (pageNum > 0)
  89.         btnNext.Enabled = (pageNum < dgrTitles.PageCount - 1)
  90.         btnLast.Enabled = (pageNum < dgrTitles.PageCount - 1)
  91.         ' Display the current page number (1-based).
  92.         txtPage.Text = (pageNum + 1).ToString
  93.     End Sub
  94.  
  95. #If PAGINGSTYLE <> 2 Then
  96.  
  97.     ' Bind the DataGrid control
  98.     ' When doing default paging, you load all the data in a DataTable
  99.     ' and let the DataGrid do the pagination automatically
  100.  
  101.     Sub BindDataGrid()
  102.         ' Retrieve the DataSet from the session variable.
  103.         Dim ds As DataSet = directcast(Session("DataSet"), DataSet)
  104.  
  105.         ' Read data from database if this is the first time we do it.
  106.         If ds Is Nothing Then
  107.             Dim cn As New OleDbConnection(BiblioConnString)
  108.             cn.Open()
  109.             ' Read data from Titles table + Name of publisher.
  110.             Dim sql As String = "SELECT Titles.*, Publishers.Name As PubName FROM Titles INNER JOIN Publishers ON Titles.PubId=Publishers.PubId"
  111.             Dim da As New OleDbDataAdapter(sql, cn)
  112.             ds = New DataSet()
  113.             da.Fill(ds, "Titles")
  114.             cn.Close()
  115.             ' Store the DataSet in a Session variable.
  116.             Session("DataSet") = ds
  117.         End If
  118.  
  119.         ' Get a DataView object sorted on the required sort expression.
  120.         Dim dv As DataView = ds.Tables("Titles").DefaultView
  121.         dv.Sort = dgrTitles.Attributes("SortExpr")
  122.         dgrTitles.DataSource = dv
  123.  
  124.         ' Do the data binding.
  125.         dgrTitles.DataBind()
  126.     End Sub
  127.  
  128. #Else
  129.  
  130.     ' when doing custom paging you must load only the data
  131.     ' strictly needed for the current page
  132.  
  133.     Sub BindDataGrid()
  134.         ' Open the connection.
  135.         Dim cn As New OleDbConnection(BiblioConnString)
  136.         cn.Open()
  137.  
  138.         ' If this isn't a post back, let's initialize the VirtualItemCount with number of records.
  139.         If Not Me.IsPostBack Then
  140.             Dim cmd As New OleDbCommand("SELECT COUNT(*) FROM Titles", cn)
  141.             dgrTitles.VirtualItemCount = CInt(cmd.ExecuteScalar)
  142.         End If
  143.  
  144.         ' Prepare to read from Titles table + Name of publisher.
  145.         Dim sql As String = "SELECT Titles.*, Publishers.Name As PubName FROM Titles INNER JOIN Publishers ON Titles.PubId=Publishers.PubId"
  146.         ' Append sort expression, if there is one.
  147.         Dim sortExpr As String = dgrTitles.Attributes("SortExpr")
  148.         If Not (sortExpr Is Nothing) AndAlso sortExpr.ToString <> "" Then
  149.             If sortExpr.StartsWith("PubName") Then
  150.                 sortExpr = "Publishers.Name" & sortExpr.Substring(7)
  151.             End If
  152.             sql &= " ORDER BY " & sortExpr
  153.         End If
  154.         ' Fill a DataTable only with data for the current page.
  155.         Dim ds As New DataSet()
  156.         Dim da As New OleDbDataAdapter(sql, cn)
  157.         da.Fill(ds, dgrTitles.CurrentPageIndex * dgrTitles.PageSize, dgrTitles.PageSize, "Titles")
  158.         ' Close the connection and release resources.
  159.         cn.Close()
  160.  
  161.         ' Do the binding.
  162.         dgrTitles.DataSource = ds.Tables("Titles")
  163.         dgrTitles.DataBind()
  164.     End Sub
  165.  
  166. #End If
  167.  
  168.     ' this event fires when a column header hyperlink is clicked
  169.  
  170.     Private Sub dgrTitles_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgrTitles.SortCommand
  171.         ' Extract current sort column and order.
  172.         Dim currSortExpr As String = dgrTitles.Attributes("SortExpr")
  173.         Dim newSortExpr As String = e.SortExpression
  174.  
  175.         ' If the sort expression is the same as the current one, just reverse the direction.
  176.         If Not (currSortExpr Is Nothing) AndAlso currSortExpr.ToString = e.SortExpression Then
  177.             newSortExpr &= " DESC"
  178.         End If
  179.  
  180.         ' Remember the new sort expression and show the first page.
  181.         dgrTitles.Attributes("SortExpr") = newSortExpr
  182.         ShowPage(0)
  183.     End Sub
  184. End Class
  185.